package cn.gdpu.action;

import cn.gdpu.service.AdminService;
import cn.gdpu.service.UserService;
import cn.gdpu.util.PageBean;
import cn.gdpu.vo.Admin;
import cn.gdpu.vo.User;


@SuppressWarnings("serial")
public class UserAction extends BaseAction {
	
	private UserService<User, Integer> userService;
	private AdminService<Admin, Integer> adminService;
	private User user;
	private Admin admin;
	private Integer id;
	
	private String username;
	private String password;
	private String repassword;
	private String oldpassword;
	private String name;
	private String age;
	private String contect;
	
	private PageBean pageBean;
	private int page;
	
	public String goLogin(){
		return "gologin";
	}
	//登陆
	public String login(){
		System.out.println("username = " + username);
		if(username != null && password != null){
			User user = userService.getUserByUsernameAndPassword(username, password);
			if(user != null){
				getSession().put("user", user);
				return "login";
			}else{
				return "gologin";
			}
		}else{
			return "gologin";
		}
	}
	
	public String logout(){
		getSession().remove("user");
		return "logout";
	}
	

	/**
	 * 添加用户,注册用
	 * 
	 * 
	 * **/
	@Override
	public String add() {
		if(username != null && !username.trim().equals("") && password != null && !password.trim().equals("") 
				&& repassword != null && !repassword.trim().equals("") ){
			if(userService.getUserByUsername(username) == null){//用户不存在
				if(password.trim().equals(repassword.trim())){
					User newUser = new User();
					newUser.setUsername(username);
					newUser.setPassword(password);
					newUser.setName(name);
					newUser.setAge(age);
					newUser.setContect(contect);
					userService.addEntity(newUser);
					getSession().put("user", newUser);
					return VIEW_PAGE;
				}
			}
		}
		return super.goAdd();
	}

	
	@Override
	public String delete() {
		Admin admin = (Admin) getSession().get("admin");
		if(admin != null){
			userService.deleteEntity(User.class, id);
			return super.delete();
		}
		return INDEX;
	}

	@Override
	public String deleteMany() {
		return super.deleteMany();
	}


	@Override
	public String goAdd() {
		return super.goAdd();
	}

	@Override
	public String goModify() {
		return super.goModify();
	}

	@Override
	public String list() {
		this.pageBean = this.userService.queryForPage(User.class, 10, page);
		if (pageBean.getList().isEmpty())
			pageBean.setList(null);
		return super.list();
	}

	@Override
	public String modify() {
		User user = (User) getSession().get("user");
		if(user != null){
			if(oldpassword != null && !oldpassword.trim().equals("") && password != null && !password.trim().equals("") 
					&& repassword != null && !repassword.trim().equals("") ){
				if(oldpassword.trim().equals(user.getPassword())){
					if(password.trim().equals(repassword.trim())){
						user = userService.getEntity(User.class, user.getId());
						user.setPassword(password);
						userService.updateEntity(user);
						return "login";
					}
				}
			}
		}
		return MODIFY_PAGE;
	}

	@Override
	public String view() {
		// TODO Auto-generated method stub
		return super.view();
	}

	
	//getter & setter
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public UserService<User, Integer> getUserService() {
		return userService;
	}
	
	public void setUserService(UserService<User, Integer> userService) {
		this.userService = userService;
	}

	public PageBean getPageBean() {
		return pageBean;
	}
	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public String getRepassword() {
		return repassword;
	}
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getContect() {
		return contect;
	}
	public void setContect(String contect) {
		this.contect = contect;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public AdminService<Admin, Integer> getAdminService() {
		return adminService;
	}
	public void setAdminService(AdminService<Admin, Integer> adminService) {
		this.adminService = adminService;
	}
	public Admin getAdmin() {
		return admin;
	}
	public void setAdmin(Admin admin) {
		this.admin = admin;
	}
	public String getOldpassword() {
		return oldpassword;
	}
	public void setOldpassword(String oldpassword) {
		this.oldpassword = oldpassword;
	}
	
}
